package com.wetao.module_demo_cyh;


import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.wetao.lib_common.AppConfig;
import com.wetao.lib_common.ConstantArouter;
import com.wetao.lib_common.utils.FileUtils;
import com.wetao.lib_common.utils.LogUtils;
import com.wetao.lib_common.view.graffiti.GraffitiOnTouchGestureListener;
import com.wetao.lib_common.view.graffiti.GraffitiTouchDetector;
import com.wetao.lib_common.view.graffiti.GraffitiView;
import com.wetao.lib_common.view.graffiti.IGraffitiTouchDetector;

import java.io.File;
import java.util.List;


@Route(path = ConstantArouter.PATH_DEMOCYH_SMARTACTIVITY)
public class SmartActivity extends Activity {

    FrameLayout fl_doodle;
    Bitmap mBitmap_b;
    Bitmap mBitmap_f;
    GraffitiView graffitiView;
    GraffitiOnTouchGestureListener mTouchGestureListener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo_cyh_smart_activity);
        fl_doodle = findViewById(R.id.fl_doodle);
        Button btn_save = findViewById(R.id.btn_save);
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                graffitiView.save();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();

        mBitmap_b = BitmapFactory.decodeResource(getResources(), R.drawable.yundie_pic);
        //mBitmap_f = BitmapFactory.decodeResource(getResources(), R.drawable.yundie_f);
        LogUtils.i("m3: mBitmap_b: width:" + mBitmap_b.getWidth() + ", height:" + mBitmap_b.getHeight());
        //LogUtils.i("m3: mBitmap_f: width:" + mBitmap_f.getWidth() + ", height:" + mBitmap_f.getHeight());

        /*String url_b_temp = FileUtils.getPicFilePath(this, "offline" + File.separator + "b", "" + 1921, "");
        Glide.with(this).asBitmap().load(url_b_temp).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        LogUtils.e("m1:Glde图片-成功  = " + url_b_temp + ", threadName = " + Thread.currentThread().getName());
                        mBitmap_b = resource;

                        initGraffitiData();
                    }

                    @Override
                    public void onLoadFailed(@Nullable Drawable errorDrawable) {
                        super.onLoadFailed(errorDrawable);
                        LogUtils.e("m1:Glde图片-失败  = " + url_b_temp + ", threadName = " + Thread.currentThread().getName());

                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {

                    }
                });*/


        graffitiView = new GraffitiView(this, mBitmap_b, mBitmap_f, false, false, new GraffitiView.IGraffitiViewListener(){

            @Override
            public void onSaved(List<Bitmap> bitmaps) {
                String path_1 = FileUtils.getPicFilePath(SmartActivity.this, "test" + File.separator, "1", "");
                String path_2 = FileUtils.getPicFilePath(SmartActivity.this, "test" + File.separator, "2", "");
                if(bitmaps.size() == 2) {
                    FileUtils.saveBitmap(bitmaps.get(0), path_1);
                    FileUtils.saveBitmap(bitmaps.get(1), path_2);
                } else if(bitmaps.size() == 1) {
                    FileUtils.saveBitmap(bitmaps.get(0), path_1);
                }
            }
        });
        mTouchGestureListener = new GraffitiOnTouchGestureListener(graffitiView, new GraffitiOnTouchGestureListener.IGraffitiOnTouchGestureListener() {
            @Override
            public void onTest() {

            }

            @Override
            public void onCallScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

            }
        });

        IGraffitiTouchDetector detector = new GraffitiTouchDetector(this, mTouchGestureListener);
        graffitiView.setDefaultTouchDetector(detector);

        mTouchGestureListener.setSupportScale(false); //禁止放大缩小
        mTouchGestureListener.setTouchType(GraffitiOnTouchGestureListener.TOUCH_TYPE_ALL);
        setSupportGraffiti(true);
        setSupportTrans(false);

        //graffitiView.setStartXy(true);
        graffitiView.setColor(Color.BLACK);
        graffitiView.setIsDrawableOutside(false);
        graffitiView.setSize(2);

        fl_doodle.addView(graffitiView);
        /*mDoodleView = new DoodleView(this, mBitmap_b, mBitmap_f);
        fl_doodle.addView(mDoodleView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);*/
    }

    private void setSupportGraffiti(boolean isSupport) {
        if (mTouchGestureListener != null) {
            mTouchGestureListener.setSupportGraffiti(isSupport);
        }
    }

    private void setSupportTrans(boolean isSupport) {
        if (mTouchGestureListener != null) {
            mTouchGestureListener.setSupportTrans(isSupport);
        }
    }

    private void setSupportDoodle(boolean isSupport) {
        if (mTouchGestureListener != null) {
            mTouchGestureListener.setSupportGraffiti(isSupport);
        }
    }

    private void otherHander() {
        /*graffitiView.save();

        mTouchGestureListener.setGraffitiTranslationXY(-10, -10);


        graffitiView.refreshMaskView(mTemp_w, mTemp_h, mGraffitiView.getWidth(), mGraffitiView.getHeight());

        mTouchGestureListener.r_onDown(mTemp_x, mTemp_y);
        mTouchGestureListener.r_onScrollBegin(mTemp_x, mTemp_y);
        mTouchGestureListener.r_onScrollEnd(mTemp_x, mTemp_y);*/

    }

    private void initGraffitiData() {
        graffitiView = new GraffitiView(this, mBitmap_b, mBitmap_f, false, false, new GraffitiView.IGraffitiViewListener(){

            @Override
            public void onSaved(List<Bitmap> bitmaps) {

            }
        });
        mTouchGestureListener = new GraffitiOnTouchGestureListener(graffitiView, new GraffitiOnTouchGestureListener.IGraffitiOnTouchGestureListener() {
            @Override
            public void onTest() {

            }
            @Override
            public void onCallScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                LogUtils.i("m1:滚动手指：distanceX: " + distanceX + ", distanceY:" + distanceY);
            }
        });

        IGraffitiTouchDetector detector = new GraffitiTouchDetector(this, mTouchGestureListener);
        graffitiView.setDefaultTouchDetector(detector);

        mTouchGestureListener.setSupportScale(false); //禁止放大缩小
        setSupportDoodle(false);
        setSupportTrans(false);
        mTouchGestureListener.setTouchType(GraffitiOnTouchGestureListener.TOUCH_TYPE_ALL);

        //graffitiView.setStartXy(true);
        graffitiView.setColor(Color.BLACK);
        graffitiView.setIsDrawableOutside(false);
        graffitiView.setSize(2);

        fl_doodle.addView(graffitiView);

    }


}
